package com.cuile.littletools


import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import com.cuile.littletools.LittleToolsContacts.CREATE_RESULT_KEY
import com.cuile.littletools.create.CreateQrDialog
import com.cuile.littletools.scan.MyCaptureActivity
import com.google.android.material.button.MaterialButton
import com.google.zxing.client.android.Intents
import com.google.zxing.integration.android.IntentIntegrator
import kotlinx.android.synthetic.main.fragment_main.*


/**
 * A simple [Fragment] subclass.
 *
 */
class MainFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


//        mainBarcodeBtn.setCompoundDrawablesWithIntrinsicBounds(null, resources.getDrawable(R.drawable.ic_bar_code_grey_800_48dp, null), null, null)
//        mainBarcodeBtn.compoundDrawablePadding = -48
//        mainBarcodeBtn.setCompoundDrawables(null, resources.getDrawable(R.drawable.ic_bar_code_grey_800_48dp, null), null, null)

        val dialog = CreateQrDialog()

        mainTextBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainContactBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainNetBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainMsgBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainWifiBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainPhoneBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainEmailBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainLocationBtn.setOnClickListener {
            showDialog(it, dialog)
        }

        mainBarcodeBtn.setOnClickListener {
            showDialog(it, dialog)
        }


        mainFloatingActionBar.setOnClickListener {
            beginScan()
        }

        val openType = activity?.intent?.getStringExtra("openType")
        if (openType != null && openType != "" && openType == "desktop") {
            beginScan()
        }
    }

    private fun beginScan() {
        val intentIntegrator = IntentIntegrator.forSupportFragment(this).setPrompt("请将二维码放入扫描框内!")
        intentIntegrator.captureActivity = MyCaptureActivity::class.java
        intentIntegrator.initiateScan()
    }

    private fun showDialog(btn: View, dialog: DialogFragment) {
        val bundle = bundleOf(LittleToolsContacts.CREATE_QR_TYPE to (btn as MaterialButton).text.toString())
        dialog.arguments = bundle
        dialog.setTargetFragment(this, LittleToolsContacts.CREATE_REQUEST_CODE)
        dialog.show(fragmentManager, CreateQrDialog::class.java.name)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == LittleToolsContacts.CREATE_REQUEST_CODE) {
            val qrContent = data?.getStringExtra(LittleToolsContacts.CREATE_RESULT_KEY)

            Log.i("MainFragment", qrContent)

            if (!qrContent.isNullOrEmpty()) {
                val bundle = bundleOf(CREATE_RESULT_KEY to qrContent)
                NavHostFragment.findNavController(this).navigate(R.id.action_mainFragment_to_qrResultFragment, bundle)
            }
        } else if (resultCode == LittleToolsContacts.LOCAL_SCANE_RESULT) {
            val scanResult =  data?.getStringExtra(Intents.Scan.RESULT)
            NavHostFragment.findNavController(this).navigate(R.id.action_mainFragment_to_scanResultFragment, bundleOf(Intents.Scan.RESULT to scanResult))
        } else {

            val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
            if (result != null) {
                if (result.contents == null) {
                    Toast.makeText(context, "扫描取消", Toast.LENGTH_SHORT).show()
                } else {
                    val bundle = bundleOf(Intents.Scan.RESULT to result.contents)
                    NavHostFragment.findNavController(this).navigate(R.id.action_mainFragment_to_scanResultFragment, bundle)
                }
            }
        }
    }

}
